using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;

/// <summary>
/// Summary description for MailUtil
/// </summary>
public class MailUtil
{
    private const string ADDRESS_FROM = "lps365.service@gmail.com";    
    private const string USER_ID = "copticalservice@gmail.com";
    private const string PASSWORD = "lps365team";
    private const string SMTP_SERVER = "smtp.gmail.com";
    private const int PORT = 587;
    private static MailMessage message;

	public MailUtil()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void send(string to, string subject, string body)
    {
        SmtpClient mailClient = new SmtpClient(SMTP_SERVER, PORT);
        mailClient.EnableSsl = true;
        NetworkCredential crendetial = new NetworkCredential(USER_ID, PASSWORD);
        mailClient.Credentials = crendetial;
        //MailMessage message = new MailMessage(ADDRESS_FROM, to, subject, body);
        message = new MailMessage(ADDRESS_FROM, to, subject, body);
        message.IsBodyHtml = true;
        //mailClient.Send(message);
        mailClient.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
        string userState = "my message";
        mailClient.Timeout = 20000;
        mailClient.SendAsync(message, userState);
    }

    public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
    {
        //Get the Original MailMessage object
        String token = (string)e.UserState;
        if (e.Cancelled)
        {
            //mailresult = "Send canceled for mail with subject " + token.ToString();
        }
        if (e.Error != null)
        {
            //mailresult = "Error " + e.Error.ToString() + " occurred";
        }
        else
        {
            //mailresult = "Message  sent.";
        }
        //mailSent = true;
        message.Dispose();
    }

}
